import axios from 'axios'
import store from '@/store'
// import store from '@/store'

// create an axios instance
const request = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 15000 // request timeout
})

// 请求拦截器
request.interceptors.request.use(
  config => {
    const token = store.getters.token
    if (token) {
      config.headers.Authorization = token
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// 响应拦截器
request.interceptors.response.use(
  response => {
    // const { data } = response
    // if (data.success) {
    //   return data
    // }
    // Message.error(data.msg)
    // return Promise.reject(data.msg)
    return response.data
  },
  error => {
    return Promise.reject(error)
  }
)

export default request
